# -*- coding: utf-8 -*-
'''
Copyright(C) 2013 "Kevin Xu <ssonxu@gmail.com>"

This file is part of acqu-center.

acqu-center is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

acqu-center is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with acqu-center.  If not, see <http://www.gnu.org/licenses/>.
'''
import re
import logging
from google.appengine.ext import ndb
import datetime

class Predef:

    MESSAGE_HELLO=u"Hello, I am acqu center robot.\n We will share some interesting news if you like."
    MESSAGE_UNHANDLED=u"Hey buddy, I don't know what are you talking about. Please speak something I knew.\n If you want help just type /help, have a good day ^_^"
    MESSAGE_MENU=u"Hello sir, you just asked for a menu of our informations:\n{}"
    MESSAGE_NOFOUND=u"Sorry, I know nothing about {}:\n, Maybe you can use /menu to confirm what are you want.\n"
    MESSAGE_NOFOUNDUSER=u"Sorry Sir, Does we know each other?.\n"
    MESSAGE_SUBS=u"Sir, your current subscriptions as follws:\n{}"
    MESSAGE_NOSUBS=u"Sir, you haven't subscribed any information yet."
    MESSAGE_HELP=u"/help - Ask for this message.\n\
            /menu - Ask for the current available informations. The items would be showed as a format as \'{ID}.{Label} - {Descriptioin}\'\n\
            /ask <ID|Lable> - Ask for detail by ID or label of the of information. Same as \"/menu\" if has no parameter be given.\n\
            /sub <ID|Label> - Subscribe an information. If no item has be indicated, it would show your subscription list.\n\
            /unsub <ID|Label> - Unsubscrbe an information. If no item has be indicated, it would show your subscription list." 

    MAX_DATA_ROWS = 1000

    doors = [ "Running Items", "Reserved Items" ]

    charsets= [ 'shift_jis' ]

    url_='http://'

    topic_='A notification from acqu-center.'

    @staticmethod
    def UTF8(s):
        return unicode(s,'utf-8')

    @staticmethod
    def genMessage(o,n):
        return u'{0}<p>{1}</p>'.format(o,n)

    @staticmethod
    def atoi(s):
        try:
            i=int(s)
        except ValueError:
            i=-1
        return i

    @staticmethod
    def isValidEmail(s):
        if s is not None:
            m = re.search(r'\w+@\w+\.\w+',s)
            if m is not None:
                return s
        else:
            raise TypeError,'inValid Email address'

    @staticmethod
    def bare_jid(sender):
        return sender.split('/')[0]

class EmailProperty(ndb.StringProperty):
    def _validate(self,value):
        Predef.isValidEmail(value)

